﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Tea;
using Tea.Utils;
using AlibabaCloud.SDK.Facebody20191230.Models;

namespace AliYun_RenLian_Obj
{

    public class Sample
    {

        /// <term><b>Description:</b></term>
        /// <description>
        /// <para>使用AK&amp;SK初始化账号Client</para>
        /// </description>
        /// 
        /// <returns>
        /// Client
        /// </returns>
        /// 
        /// <term><b>Exception:</b></term>
        /// Exception
        public static AlibabaCloud.SDK.Facebody20191230.Client CreateClient()
        {
            // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
            // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378671.html。
            //AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            //{
            //    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            //    AccessKeyId = Environment.GetEnvironmentVariable("LTAI5tA1goPxJXZV3urxxig9"),
            //    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            //    AccessKeySecret = Environment.GetEnvironmentVariable("Yxiz5MW4NSx6zjjuiTOov0EhiXeccm"),
            //};
            string AccessKeyId = System.Configuration.ConfigurationManager.ConnectionStrings["RenLian_AccessKeyId"].ConnectionString.ToString();
            string AccessKeySecret = System.Configuration.ConfigurationManager.ConnectionStrings["RenLian_AccessKeySecret"].ConnectionString.ToString();

            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = AccessKeyId,
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = AccessKeySecret,
            };
            // Endpoint 请参考 https://api.aliyun.com/product/facebody
            config.Endpoint = "facebody.cn-shanghai.aliyuncs.com";
            return new AlibabaCloud.SDK.Facebody20191230.Client(config);
        }

        public Res_msg Main(string base64A, string base64B)
        {
            Res_msg res_Msg = new Res_msg();
            res_Msg.code = 1;
            AlibabaCloud.SDK.Facebody20191230.Client client = CreateClient();
            AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceRequest compareFaceRequest = new AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceRequest
            {
                //ImageURLA = base64A,
                //ImageURLB = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFace/CompareFace-left1.png",

                ImageDataA = base64A,
                ImageDataB = base64B,

            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceResponse resp = client.CompareFaceWithOptions(compareFaceRequest, runtime);

                res_Msg.Confidence = resp.Body.Data.Confidence;
                return res_Msg;
                //AlibabaCloud.TeaConsole.Client.Log(AlibabaCloud.TeaUtil.Common.ToJSONString(resp));
            }
            catch (TeaException error)
            {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                res_Msg.code = 0;
                res_Msg.Message = error.Message;
                return res_Msg;
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                res_Msg.code = 0;
                res_Msg.Message = error.Message;
                return res_Msg;
            }
        }


    }
    public class Res_msg
    {/// <summary>
     /// 错误信息
     /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 置信度（Confidence）超过 61%就可以认为是同一个人。
        /// </summary>
        public float? Confidence { get; set; }
        public int? code { get; set; }
        public int? StatusCode { get; internal set; }
        public List<SearchFaceResponseBody.SearchFaceResponseBodyData.SearchFaceResponseBodyDataMatchList.SearchFaceResponseBodyDataMatchListFaceItems> data { get; internal set; }
    }
}
